package com.yanqu.road.server.cmd.activity.suoyao;

import com.yanqu.road.entity.activity.suoyao.SuoYaoNotice;
import com.yanqu.road.logic.pb.SuoYaoPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.SuoYaoActivityProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.suoyao.SuoYaoActivity;
import com.yanqu.road.server.manager.suoyao.SuoYaoMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = CrossProtocol.C2_SUOYAO_ACTIVITY_GET_NOTICE, desc = "跨服拿锁妖塔公告")
public class CrossSuoYaoGetNoticeCmd extends CrossCommand {

    private int activityId;
    private long serverId;
    private long userId;

    private List<SuoYaoNotice> noticeList = new ArrayList<>();

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        SuoYaoActivityProto.CrossGetSuoYaoNoticeReqMsg reqMsg = SuoYaoActivityProto.CrossGetSuoYaoNoticeReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        serverId = reqMsg.getServerId();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        SuoYaoActivity activity = SuoYaoMgr.getSuoYaoActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_SUOYAO_ACTIVITY_NO_OPEN;
            return;
        }

        noticeList = activity.getSuoYaoNoticeData();
    }

    @Override
    public void sendData() {
        SuoYaoActivityProto.GetSuoYaoNoticeRespMsg.Builder builder = SuoYaoActivityProto.GetSuoYaoNoticeRespMsg.newBuilder();
        builder.setRet(ret);
        for (SuoYaoNotice suoYaoNotice : noticeList) {
            builder.addNoticeList(SuoYaoPb.parseSuoYaoNoticeMsg(suoYaoNotice));
        }
        MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(ClientProtocol.U_SUOYAO_ACTIVITY_GET_NOTICE, builder));
    }
}
